package com.review.cli;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.review.analysis.StaticAnalysisService;
import com.review.graph.CallGraphService;
import com.review.model.CallGraph;
import com.review.model.CallGraphBuildRequest;
import com.review.model.SarifModels;
import picocli.CommandLine.Command;
import picocli.CommandLine.Option;

import java.io.File;

@Command(name = "project", description = "Analyze the entire project: call graph + static analysis")
public class ProjectAnalyzeCommand implements Runnable {

	@Option(names = "--path", defaultValue = ".", description = "Project root path")
	private String projectPath;

	@Option(names = "--precision", defaultValue = "CHA", description = "Call graph precision (CHA|RTA placeholder)")
	private String precision;

	@Override
	public void run() {
		try {
			ObjectMapper om = new ObjectMapper();
			CallGraphService cg = new CallGraphService();
			CallGraph graph = cg.build(new CallGraphBuildRequest(projectPath, precision));

			StaticAnalysisService sa = new StaticAnalysisService();
			SarifModels.SarifRun semgrep = null;
			try { semgrep = sa.runSemgrep(new File(projectPath)); } catch (Exception ignored) {}

			var output = om.createObjectNode();
			output.set("callGraph", om.valueToTree(graph));
			if (semgrep != null) output.set("semgrep", om.valueToTree(semgrep));
			System.out.println(om.writerWithDefaultPrettyPrinter().writeValueAsString(output));
		} catch (Exception e) {
			throw new RuntimeException(e);
		}
	}
}

